<?php

namespace app\api\controller;

use app\common\model\Attachment as AttachmentModel;
use app\model\Member as MemberModel;
use think\Db;
use think\facade\Log;
use think\facade\Request;
use think\helper\Hash;

/**
 * @title   用户信息
 * @desc    用户信息
 * Class Link
 * @package app\api\controller
 */

class Member extends Common
{
    /**
     * @title 用户信息
     * @desc  用户信息
     *
     * @param int $title
     * @param int $limit 10
     *
     * @return Array $list 用户信息
     */
    public function info()
    {
        // try {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $miniuid = $this->request->param('miniuid');
        if (!$miniuid) {
            $miniuid = '123456';
        }
        $limit = $this->request->param('limit');
        if (!$limit) {
            $limit = 1;
        }
        if ($miniuid != '') {
            $map[] = ['miniuid', '=', $miniuid];
        }
        $list = MemberModel::where($map)
            ->field(['id', 'account', 'nickname'])
            ->order('id desc')
            ->paginate($limit);

        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }

    public function loginmnid()
    {
        $limit = $this->request->param('limit');
        if (!$limit) {
            $limit = 1;
        }
        $miniuid = $this->request->param('miniuid');
        if (!$miniuid) {
            $miniuid = '123456';
        }
        if ($miniuid != '') {
            $map[] = ['miniuid', '=', $miniuid];
        }
        $list = MemberModel::where($map)
            ->field(['id', 'account', 'nickname'])
            ->paginate($limit);

        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }

    public function bindmnid()
    {
        $account = $this->request->param('username');
        $limit = $this->request->param('limit');
        if (!$limit) {
            $limit = 1;
        }
        $miniuid = $this->request->param('miniuid');
        if (!$miniuid) {
            $miniuid = '123456';
        }
        //在模型中更新数据库，把miniuid更新到member表
        if ($account != '') {
            $map[] = ['account', '=', $account];
        }
        if ($miniuid != '') {
            $map[] = ['miniuid', '=', $miniuid];
        }

        $list = MemberModel::where($map)
            ->field(['id', 'account', 'nickname'])
            ->paginate($limit);

        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }

    public function loginuser()
    {
        $account = $this->request->param('username');
        $password = $this->request->param('password');
        //$miniuid = $this->request->param('miniuid');
        $limit = $this->request->param('limit');
        if (!$limit) {
            $limit = 1;
        }
        if ($account != '') {
            $map[] = ['account', '=', $account];
        }
        if ($password != '') {
            $map[] = ['password', '=', $password]; //Hash::make((string) $password)
        }
        $list = MemberModel::where($map)
            ->field(['id', 'account', 'nickname'])
            ->paginate($limit);

        return $this->jsonPage($list->total(), $list->toArray()['data']);
    }
}
